﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APIBetaSeries.Managers;
using APIBetaSeries.Models;
using APIBetaSeries.Models.Member;
using APIBetaSeries.Models.Status;

namespace APIBetaSeries.Test
{
    public partial class Form1 : Form
    {
        private Member _member;

        public Form1()
        {
            InitializeComponent();
            groupBox1.Visible = false;
            values.Visible = false;
            groupBox2.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Status reponse = StatusManager.Instance.GetStatus();
            StringBuilder str = null;
            if (reponse.Code == 0)
            {
                str = new StringBuilder("Erreurs :");
                foreach (Error error in reponse.Errors)
                {
                    str.AppendLine(" - Erreur :");
                    str.AppendLine(String.Format("   - Code    : {0}", error.Code));
                    str.AppendLine(String.Format("   - Content : {0}", error.Content));
                }
            }
            else
            {
                str = new StringBuilder("Status :");
                str.AppendLine();
                str.AppendLine(String.Format(" - Status   : {0}", reponse.WebSiteStatus.Status));
                str.AppendLine(String.Format(" - Database : {0}", reponse.WebSiteStatus.Database));
                str.AppendLine();
                str.AppendLine("Api :");
                str.AppendLine();
                str.AppendLine(String.Format(" - Version : {0}", reponse.ApiStatus.Version));
                str.AppendLine("Changes :");
                str.AppendLine();
                foreach (VersionStatus versionStatus in reponse.ApiStatus.VersionStatusList)
                {
                    str.AppendLine(String.Format(" - Date : {0}", versionStatus.Date));
                    str.AppendLine(" - Commentaire(s) :");
                    foreach (ChangeStatus changeStatus in versionStatus.ChangeStatusList)
                    {
                        str.AppendLine(String.Format("   - {0} - {1}", changeStatus.Type, changeStatus.Value));
                    }
                }
            }

            ConsoleText.Text = str.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String[] valeurs = values.Text.Split(';');
            if (valeurs.Count() < 2)
                MessageBox.Show("Nécessite 2 valeurs : Login;MotDePasse");
            else
            {
                Member reponse = MemberManager.Instance.Connect(valeurs[0], valeurs[1], false);
                StringBuilder str = null;
                if (reponse.Code == 0)
                {
                    str = new StringBuilder("Erreurs :");
                    str.AppendLine();
                    foreach (Error error in reponse.Errors)
                    {
                        str.AppendLine(" - Erreur :");
                        str.AppendLine(String.Format("   - Code    : {0}", error.Code));
                        str.AppendLine(String.Format("   - Content : {0}", error.Content));
                    }
                }
                else
                {
                    str = new StringBuilder("Utilisateur :");
                    str.AppendLine();
                    str.AppendLine(String.Format(" - Login : {0}", reponse.Login));
                    str.AppendLine(String.Format(" - Token : {0}", reponse.Token));
                    _member = reponse;
                }

                ConsoleText.Text = str.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String valeur = values.Text;
            String token;
            if (_member == null && String.IsNullOrEmpty(valeur))
            {
                if (String.IsNullOrEmpty(valeur))
                {
                    MessageBox.Show("Veuillez saisir le token de déconnexion");
                }
                else if (_member == null)
                    MessageBox.Show("Veuillez vous connecter avant de vous déconnecter");
            }
            else
            {
                token = !String.IsNullOrEmpty(valeur) ? valeur : _member != null ? _member.Token : String.Empty;
                Member reponse = MemberManager.Instance.Disconnect(token);
                StringBuilder str = null;
                if (reponse.Code == 0)
                {
                    str = new StringBuilder("Erreurs :");
                    str.AppendLine();
                    foreach (Error error in reponse.Errors)
                    {
                        str.AppendLine(" - Erreur :");
                        str.AppendLine(String.Format("   - Code    : {0}", error.Code));
                        str.AppendLine(String.Format("   - Content : {0}", error.Content));
                    }
                }
                else
                {
                    str = new StringBuilder("SUCCESS!!!");
                    _member = null;
                }

                ConsoleText.Text = str.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String valeur = values.Text;
            if (String.IsNullOrEmpty(valeur))
                MessageBox.Show("Veuillez saisir le token à tester");
            else
            {
                Member reponse = MemberManager.Instance.IsActive(valeur);
                StringBuilder str = null;
                if (reponse.Code == 0)
                {
                    str = new StringBuilder("Erreurs :");
                    str.AppendLine();
                    foreach (Error error in reponse.Errors)
                    {
                        str.AppendLine(" - Erreur :");
                        str.AppendLine(String.Format("   - Code    : {0}", error.Code));
                        str.AppendLine(String.Format("   - Content : {0}", error.Content));
                    }
                }
                else
                {
                    str = new StringBuilder("VALIDE!!!");
                    _member = null;
                }

                ConsoleText.Text = str.ToString();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(values.Text))
                MessageBox.Show("Valeurs attendu: Login;Nodata?;since?");
            else
            {
                String[] valeur = values.Text.Split(';');
                Boolean? noData = valeur.Count() >= 2 ? (Boolean?)Convert.ToBoolean(valeur[1]) : null;
                String timeStamp = valeur.Count() == 3 ? valeur[2] : null;
                Member reponse = MemberManager.Instance.GetInfos(valeur[0], noData, timeStamp);
                StringBuilder str = null;
                if (reponse.Code == 0)
                {
                    str = new StringBuilder("Erreurs :");
                    str.AppendLine();
                    foreach (Error error in reponse.Errors)
                    {
                        str.AppendLine(" - Erreur :");
                        str.AppendLine(String.Format("   - Code    : {0}", error.Code));
                        str.AppendLine(String.Format("   - Content : {0}", error.Content));
                    }
                }
                else
                {

                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            values.Visible = false;
            groupBox2.Visible = false;
            if (comboBox1.SelectedItem.ToString() == "Status")
                groupBox1.Visible = true;
            if (comboBox1.SelectedItem.ToString() == "Member")
            {
                values.Visible = true;
                groupBox2.Visible = true;
            }
        }
    }
}
